package com.sakila.action.payment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sakila.action.BaseAction;
import com.sakila.action.util.JsonUtil;
import com.sakila.pojo.Member;
import com.sakila.pojo.Payment;
import com.sakila.pojo.other.Page;
import com.sakila.service.PaymentService;
import com.sakila.vo.MyMember;
import com.sakila.vo.PaymentVo;

@Controller
public class PaymentAction implements BaseAction{
	public static final Logger logger = LoggerFactory.getLogger(PaymentAction.class);
	@Autowired
	private PaymentService paymentService;
	
	@RequestMapping(value="/staff/goto_viewOrder")
	public String Root(){
		return "staff/rental/index";
	}
	
	@RequestMapping(value={"/member/viewOrder"})
	@ResponseBody
	public Object viewOwnList(
			@RequestParam(value=PAGE_ID,defaultValue="1")int pageId,
			@RequestParam(value=PAGE_SIZE,defaultValue="30")int pageSize,
			Authentication au,
			//for toolBar searching, when sorting, easyui will do the request contain these fields at the same time
			@RequestParam(required=false)Date startDate,
			@RequestParam(required=false)Date endDate){
		Member member = ((MyMember)au.getPrincipal()).getMember();
		PaymentVo pv = paymentService.findByPeriod(new Page(pageId, pageSize), member, startDate, endDate);
		return JsonUtil.toJson(pv.getList(), pv.getTotalNumOfRows());
	}
	
	@RequestMapping(value={"/staff/viewOrder"})
	@ResponseBody
	public Object viewAllList(
			@RequestParam(value=PAGE_ID,defaultValue="1")int pageId,
			@RequestParam(value=PAGE_SIZE,defaultValue="30")int pageSize,
			Authentication au,
			//for toolBar searching, when sorting, easyui will do the request contain these fields at the same time
			@RequestParam(required=false)Date startDate,
			@RequestParam(required=false)Date endDate,
			@RequestParam(required=false)Integer memberId){
		Member member = null;
		if(memberId != null){
			member = new Member();
			member.setId(memberId);
		}
		PaymentVo pv = paymentService.findByPeriod(new Page(pageId, pageSize), member, startDate, endDate);
		return JsonUtil.toJson(pv.getList(), pv.getTotalNumOfRows());
	}
	
	@RequestMapping(value="/staff/viewOrderById")
	@ResponseBody
	public Object viewSelectedOrder(@RequestParam Integer paymentId){
		List<Payment> list = new ArrayList<Payment>();
		Payment payment = paymentService.findById(paymentId);
		list.add(payment);
		return JsonUtil.toJson(list);
	}
	
	@RequestMapping(value={"/staff/viewUnreturned", "/staff/viewUnreturned/{memberId}"})
	@ResponseBody
	public Object viewUnreturnedList(@RequestParam(value=PAGE_ID,defaultValue="1")int pageId,
									@RequestParam(value=PAGE_SIZE,defaultValue="30")int pageSize,
									@PathVariable Integer memberId){
		Member member = null;
		if(memberId != null){
			member = new Member();
			member.setId(memberId);
		}
		PaymentVo pv = paymentService.findUnReturned(new Page(pageId, pageSize), member);
		return JsonUtil.toJson(pv.getList(), pv.getTotalNumOfRows());
	}
	
	@RequestMapping(value="/staff/finishOrder/{paymentId}")
	@ResponseBody
	public String finishOrder(@PathVariable Integer paymentId){
		paymentService.finishOrder(paymentId);
		return "ok";
	}
}
